﻿using log4net;
using SQLDataUploadHelper.daos.bases;
using SQLDataUploadHelper.models;
using SQLDataUploadHelper.models.domain;
using SQLDataUploadHelper.services;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SQLDataUploadHelper.daos
{
    internal class SqlConnectConfigDao : SqliteBaseDao
    {
        private readonly ILog log = LogManager.GetLogger(App.LogRepository.Name, typeof(SqlConnectConfigDao));
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <returns></returns>
        public SqlConnectConfig getOne()
        {
            string sql = "select * from mysql_connect_config";
            SQLiteDataReader sqliteDataReader = SqliteDatabase.runSelectSql(sql);
            SqlConnectConfig sqlConnectConfig = new SqlConnectConfig();
            if (sqliteDataReader != null)
            {
                if (sqliteDataReader.Read())
                {
                    sqlConnectConfig.Id = Convert.ToInt64(sqliteDataReader["id"]);
                    sqlConnectConfig.Ip = sqliteDataReader["ip"].ToString();
                    sqlConnectConfig.DatabaseName = sqliteDataReader["database_name"].ToString();
                    sqlConnectConfig.UserName = sqliteDataReader["user_name"].ToString();
                    sqlConnectConfig.Password = sqliteDataReader["password"].ToString();
                }
            }
            log.Debug(sql);
            return sqlConnectConfig;
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="sqlConnectConfig"></param>
        /// <returns></returns>
        public bool update(SqlConnectConfig sqlConnectConfig)
        {
            string sql = string.Format("" +
                "update mysql_connect_config " +
                "set ip='{0}', database_name='{1}', user_name='{2}', password='{3}' " +
                "where id={4}", sqlConnectConfig.Ip, sqlConnectConfig.DatabaseName,
                sqlConnectConfig.UserName,
                sqlConnectConfig.Password, sqlConnectConfig.Id);
            log.Debug(sql);
            return SqliteDatabase.execUpdateSql(sql);
        }
    }
}
